package cn.weakup.thread.question._volatile;

/**
 * @author Dawn
 * @version 1.0.0
 * @desc
 * @date 2022/5/10
 */
public class VolatileExample {
    int x = 0;
    volatile boolean v = false;

    public void writer() {
        x = 1;
        v = true;
    }

    public void reader() {
        if (v == true) {
            //x的值是多少呢？
            System.out.println(x);
        }
    }

    public static void main(String[] args) {
        VolatileExample volatileExample = new VolatileExample();


        Thread threadA = new Thread(() -> {
            volatileExample.writer();
        });

        Thread threadB = new Thread(() -> {
            volatileExample.reader();
        });

        threadA.start();
        threadB.start();
    }
}
